﻿using System;
using System.Data;
using System.Data.Common;
using XianShang.Common;
using XianShang.Entity;
namespace XianShang.Data.SqlServer
{
    public partial class DataProvider : IDataProvider
    {
        /// <summary>
        /// 增加一條使用说明信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int AddHelpInfo(HelpInfo Hinfo)
        {
            DbParameter[] getPar = {
                                       
                                       DbHelper.MakeInParam("@Helptype",(DbType)SqlDbType.NVarChar,50,Hinfo.HelpType),
                                       DbHelper.MakeInParam("@Title",(DbType)SqlDbType.NVarChar,300,Hinfo.Title),
                                       DbHelper.MakeInParam("@Content",(DbType)SqlDbType.NText,1073741823,Hinfo.Content),
                                        DbHelper.MakeInParam("@HelpOrder",(DbType)SqlDbType.TinyInt,5,Hinfo.HelpOrder),
                                       DbHelper.MakeInParam("@Time",(DbType)SqlDbType.DateTime,DateTime.Now.ToString().Length,Hinfo.Time)
                                   };
            string commandText = "INSERT INTO [dbo].[XS_Help]([HelpType],[Title],[Content],[Hits],[helporder],[Time]) VALUES(@Helptype,@Title,@Content,0,@helporder,@Time)";

            return Utils.StrToInt(DbHelper.ExecuteNonQuery(CommandType.Text, commandText, getPar), -1);
        }

        /// <summary>
        /// 增加一條使用说明信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int AddHtmlHelpInfo(HelpInfo Hinfo)
        {
            DbParameter[] getPar = {
                                       DbHelper.MakeInParam("@Title",(DbType)SqlDbType.NVarChar,300,Hinfo.Title),
                                       DbHelper.MakeInParam("@Helptype",(DbType)SqlDbType.NVarChar,50,Hinfo.HelpType),
                                       DbHelper.MakeInParam("@url",(DbType)SqlDbType.NVarChar,300,Hinfo.Url),
                                        DbHelper.MakeInParam("@HelpOrder",(DbType)SqlDbType.TinyInt,5,Hinfo.HelpOrder),
                                       DbHelper.MakeInParam("@Time",(DbType)SqlDbType.DateTime,DateTime.Now.ToString().Length,DateTime.Now)
                                   };
            string commandText = "INSERT INTO [dbo].[XS_Help]([HelpType],[Title],[Url],[Hits],[Helporder],[Time]) VALUES(@Helptype,@Title,@url,0,@helporder,@Time)";

            return Utils.StrToInt(DbHelper.ExecuteNonQuery(CommandType.Text, commandText, getPar), -1);
        }

        /// <summary>
        /// 獲取信息顯示使用说明列表
        /// </summary>
        /// <returns></returns>
        public DataSet GetHelpInfo()
        {
            string commandText = "SELECT * FROM [dbo].[XS_Help] order by [Time] desc,[helporder] asc";

            return DbHelper.ExecuteDataset(CommandType.Text, commandText);
        }


        /// <summary>
        ///  獲取一條要修改的使用说明信息顯示
        /// </summary>
        /// <returns></returns>
        public HelpInfo GetHelpId(string HId)
        {
            DbParameter getPar = DbHelper.MakeInParam("@Id", (DbType)SqlDbType.Int, int.MaxValue, HId);
            string commandText = "SELECT * FROM [dbo].[XS_Help] WHERE Id=@Id";
            HelpInfo Hinf = new HelpInfo();
            DataSet ds = DbHelper.ExecuteDataset(CommandType.Text, commandText, getPar);
            if (ds.Tables[0].Rows.Count > 0)
            {
                Hinf.ID = Utils.objToInt64(ds.Tables[0].Rows[0]["Title"], -1);
                Hinf.Title = Utils.objToStr(ds.Tables[0].Rows[0]["Title"]);
                Hinf.Content = Utils.objToStr(ds.Tables[0].Rows[0]["Content"]);
                Hinf.Time = Utils.objToStr(ds.Tables[0].Rows[0]["Time"]);
                Hinf.HelpType = Utils.objToStr(ds.Tables[0].Rows[0]["helptype"]);
                Hinf.HelpOrder=Utils.StrToInt(ds.Tables[0].Rows[0]["helporder"],0);
                Hinf.Url = Utils.objToStr(ds.Tables[0].Rows[0]["url"]);
            }
            return Hinf;

        }


        /// <summary>
        /// 更新一條要修改的使用说明信息
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public int UpHelpInfo(string Title, string Content, string Id)
        {
            DbParameter[] getPar = {
                                       DbHelper.MakeInParam("@Title",(DbType)SqlDbType.NVarChar,300,Title),
                                       DbHelper.MakeInParam("@Content",(DbType)SqlDbType.NText,1073741823,Content),
                                     //  DbHelper.MakeInParam("@Time",(DbType)SqlDbType.Time,DateTime.Now.ToString().Length,Time),
                                       DbHelper.MakeInParam("@Id",(DbType)SqlDbType.Int,int.MinValue,Id)
                                   };
            string commandText = "UPDATE [dbo].[XS_Help] SET [Title]=@Title,[Content]=@Content where [Id]=@Id";

            return Utils.StrToInt(DbHelper.ExecuteNonQuery(CommandType.Text, commandText, getPar), -1);
        }
        /// <summary>
        /// 更新一條要修改的使用说明信息
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public int UpHelpByHtmlInfo(HelpInfo hInfo)
        {
            DbParameter[] getPar = {
                                       DbHelper.MakeInParam("@Title",(DbType)SqlDbType.NVarChar,300,hInfo.Title),
                                       DbHelper.MakeInParam("@Url",(DbType)SqlDbType.NVarChar,300,hInfo.Url),
                                     //  DbHelper.MakeInParam("@Time",(DbType)SqlDbType.Time,DateTime.Now.ToString().Length,Time),
                                       DbHelper.MakeInParam("@Id",(DbType)SqlDbType.Int,int.MinValue,hInfo.ID)
                                   };
            string commandText = "UPDATE [dbo].[XS_Help] SET [Title]=@Title,[Url]=@Url where [Id]=@Id";

            return Utils.StrToInt(DbHelper.ExecuteNonQuery(CommandType.Text, commandText, getPar), -1);
        }
        /// <summary>
        /// 根據使用说明ID刪除使用说明信息
        /// </summary>
        /// <returns></returns>
        public int DelHelpInfo(string Id)
        {
            DbParameter getPar = DbHelper.MakeInParam("@Id", (DbType)SqlDbType.Int, int.MaxValue, Id);

            string commandText = "DELETE FROM [dbo].[XS_Help] WHERE [Id]=@Id";

            return DbHelper.ExecuteNonQuery(CommandType.Text, commandText, getPar);
        }
    }
}
